<template>
  <div class="login-wrap">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="form">
      <h4>登录</h4>
      <el-form-item prop="username">
        <el-input
          type="text"
          prefix-icon="el-icon-user"
          placeholder="请输入用户名"
          v-model="ruleForm.username"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          prefix-icon="el-icon-lock"
          v-model="ruleForm.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <div class="btns">
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
          :loading="loading"
          >登录</el-button
        >
        <el-button type="success" plain @click="resetForm('ruleForm')"
          >重置</el-button
        >
        <el-button type="success" plain @click="gotoRegister()">注册</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import socket_m from "../util/connect";
import { getAllGroups_fn } from "../api/Index/group";
export default {
  data() {
    return {
      loading: false,
      ruleForm: {},
      rules: {
        username: [
          { required: true, message: "必须填写用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "必须填写密码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 执行登录
          this.loading = true;
          await socket_m.connectServer();
          socket_m.sendMsg("login", this.ruleForm, (o) => {
            if (o.code === 0) {
              // 登录成功
              localStorage.setItem("token", o.token);
              localStorage.setItem("nick", o.nick);
              localStorage.setItem("avatarUrl", o.avatarUrl);
              this.$store.commit("updateNick_fn", o.nick);
              localStorage.setItem("uid", o.uid);
              this.$store.commit("setConnectSuccess_fn");
              this.$router.replace("/index");
              console.log("登录成功");
              console.log(o);
              // 获取所有加入的群信息
              // getAllGroups_fn();
            } else if (o.code === 1) {
              this.$message.error("用户不存在");
            } else if (o.code === 2) {
              this.$message.error("密码错误");
            } else {
              this.$message.error("登录失败");
            }
          });
          // 用io进行登录
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    gotoRegister() {
      this.$router.replace("/register");
    },
  },
};
</script>
<style lang="less" scoped>
.login-wrap {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .form {
    padding: 20px 30px;
    width: 300px;
    height: 200px;
    border: 1px solid #ccc;
    border-radius: 10px;
    h4 {
      margin-bottom: 10px;
      text-align: center;
    }
    .btns {
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>